package com.daiyang.lease.web.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.daiyang.lease.common.exception.LeaseException;
import com.daiyang.lease.common.result.ResultCodeEnum;
import com.daiyang.lease.model.entity.*;
import com.daiyang.lease.model.enums.ItemType;
import com.daiyang.lease.web.admin.mapper.*;
import com.daiyang.lease.web.admin.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.daiyang.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.daiyang.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.daiyang.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.daiyang.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.daiyang.lease.web.admin.vo.fee.FeeValueVo;
import com.daiyang.lease.web.admin.vo.graph.GraphVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private GraphInfoService  graphInfoService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private ApartmentLabelService  apartmentLabelService;
    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private  LabelInfoMapper labelInfoMapper;
    @Autowired
    private  FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private  FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Override
   public void saveOrUpdateApartment(ApartmentSubmitVo apartmentSubmitVo) {
        //更新或保存公寓信息
        this.saveOrUpdate(apartmentSubmitVo);
        boolean isUpdate = apartmentSubmitVo.getId() != null;
        Long id = apartmentSubmitVo.getId();
        if(isUpdate) {
            // 删除图片信息
            graphInfoService.remove(
                    new LambdaQueryWrapper<GraphInfo>()
                            .eq(GraphInfo::getItemId,id)
                            .eq(GraphInfo::getItemType, ItemType.APARTMENT));

            // 删除公寓配套关系表里信息
            apartmentFacilityService.remove(
                    new LambdaQueryWrapper<ApartmentFacility>()
                            .eq(ApartmentFacility::getApartmentId,id));


            // 删除公寓费用值关系表里信息
            apartmentFeeValueService.remove(
                    new LambdaQueryWrapper<ApartmentFeeValue>()
                            .eq(ApartmentFeeValue::getApartmentId,id));

            // 删除公寓标签关系表里信息
            apartmentLabelService.remove(
                    new LambdaQueryWrapper<ApartmentLabel>()
                         .eq(ApartmentLabel::getApartmentId,id));

        }


        // 添加图片信息
        List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
        if(!CollectionUtils.isEmpty(graphVoList)) {
            //转化
            List<GraphInfo> graphInfoList =new ArrayList<>();
            for(GraphVo graphVo:graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setItemId(id);
                graphInfo.setItemType(ItemType.APARTMENT);
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }


        // 添加公寓配套关系表里信息
        List< Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
        if(!CollectionUtils.isEmpty(facilityInfoIds)) {
            List<ApartmentFacility> apartmentFacilityList =new ArrayList<>();
            for(Long facilityInfoId:facilityInfoIds) {
                ApartmentFacility apartmentFacility = ApartmentFacility.builder()
                        .apartmentId(id)
                        .facilityId(facilityInfoId)
                        .build();
                apartmentFacilityList.add(apartmentFacility);
            }
            apartmentFacilityService.saveBatch(apartmentFacilityList);
        }


        //4.插入杂费列表
        List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
        if (!CollectionUtils.isEmpty(feeValueIds)) {
            ArrayList<ApartmentFeeValue> apartmentFeeValueList = new ArrayList<>();
            for (Long feeValueId : feeValueIds) {
                ApartmentFeeValue apartmentFeeValue = ApartmentFeeValue.builder()
                        .apartmentId(id)
                        .feeValueId(feeValueId)
                        .build();
                apartmentFeeValueList.add(apartmentFeeValue);
            }
            apartmentFeeValueService.saveBatch(apartmentFeeValueList);
        }
        // 添加公寓标签关系表里信息
        List< Long> labelIds = apartmentSubmitVo.getLabelIds();
        if (!CollectionUtils.isEmpty(labelIds)){
            List<ApartmentLabel> apartmentLabelList =new ArrayList<>();
            for(Long labelId:labelIds) {
                ApartmentLabel apartmentLabel = ApartmentLabel.builder()
                        .apartmentId(id)
                        .labelId(labelId)
                        .build();
                apartmentLabelList.add(apartmentLabel);
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }


    }

    @Override
    public IPage<ApartmentItemVo> pageApartmentItemByQuery(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.pageApartmentItemByQuery(page, queryVo);
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        //1.查询ApartmentInfo
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo == null) {
            return null;
        }

        //2.查询图片
        List<GraphVo >  graphVoList=graphInfoMapper.selectListByItemTypeAndId(ItemType.APARTMENT, id);

        //查询标签
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartmentId(id);

        //4.查询FacilityInfo
        List<FacilityInfo> facilityInfoList =
                facilityInfoMapper.selectListByApartmentId(id);

        //5.查询FeeValue
        List<FeeValueVo> feeValueVoList = feeValueMapper.selectListByApartmentId(id);

        ApartmentDetailVo adminApartmentDetailVo = new ApartmentDetailVo();
        BeanUtils.copyProperties(apartmentInfo, adminApartmentDetailVo);
        adminApartmentDetailVo.setGraphVoList(graphVoList);
        adminApartmentDetailVo.setLabelInfoList(labelInfoList);
        adminApartmentDetailVo.setFacilityInfoList(facilityInfoList);
        adminApartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return adminApartmentDetailVo;
    }

    @Override
    public void removeApartmentById(Long id) {
        LambdaQueryWrapper<RoomInfo> roomQueryWrapper = new LambdaQueryWrapper<>();
        roomQueryWrapper.eq(RoomInfo::getApartmentId, id);
        Long count = roomInfoMapper.selectCount(roomQueryWrapper);
        if (count > 0) {
    //直接为前端返回如下响应：先删除房间信息再删除公寓信息
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        this.removeById(id);
        // 1.删除图片信息
        graphInfoService.remove(new LambdaQueryWrapper<GraphInfo>()
                .eq(GraphInfo::getItemId,id)
                .eq(GraphInfo::getItemId,ItemType.APARTMENT));

        //2.删除公寓配套关系表里信息
        apartmentFacilityService.remove(new LambdaQueryWrapper<ApartmentFacility>()
                .eq(ApartmentFacility::getApartmentId,id)
                );
        //3.删除ApartmentLabel
        apartmentLabelService.remove(new LambdaQueryWrapper<ApartmentLabel>()
                .eq(ApartmentLabel::getApartmentId,id));
        //4.删除ApartmentFeeValue
        LambdaQueryWrapper<ApartmentFeeValue> feeQueryWrapper = new
                LambdaQueryWrapper<>();
        feeQueryWrapper.eq(ApartmentFeeValue::getApartmentId, id);
        apartmentFeeValueService.remove(feeQueryWrapper);

    }


}




